/**----------------------------------------
 * @description native 和业务交互类
 * @author      lxc
 * @file        NativeBridge.ts
 * @Date        2022-05-05
 * @version     1.0.0
 * ----------------------------------------
 */

import { LogClass } from "./LogClass";

export const IS_IOS = cc.sys.os === cc.sys.OS_IOS || cc.sys.os === cc.sys.OS_OSX

export class NativeBridge extends LogClass {

    bridgeFunc: Function = null;

    callNative() {
        if (CC_JSB) {
            if (IS_IOS) {
                this.bridgeFunc = function (messageName: string, jsonString: string) {
                    // Java 的类名
                    let className = "org/cocos2dx/lib/JavaToEnginBridge";
                    // Java 方法名
                    let methodName = "JavaRecvEnginMsgFunc"
                    /**
                     * Java 方法的参数列表为(string,string):void
                     * @description 参数类型定义参考书本中有写
                     */
                    let methodSignature = "(Ljava/lang/String;Ljava/lang/String;)V"
                    // 在 java 需要实现对应的文件和方法
                    jsb.reflection.callStaticMethod(className, methodName, methodSignature, messageName, jsonString)
                }
            } else {
                // 将对象转换成 json 字符串后传递至 Native
                this.bridgeFunc = function (messageName: string, jsonString: string) {
                    // 在 oc 需要实现对应的文件和方法
                    jsb.reflection.callStaticMethod("IosToEnginBridge","IosRecvEnginMsgFunc",messageName,jsonString)
                }
            }
        }
    }
}